package com.enwei.activitys;


import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Text;

import com.enwei.adapters.ShoppingCartAdapter;
import com.enwei.base.BaseActivity;
import com.enwei.model.ProducInfoModel;
import com.ew.action.R;
import com.lidroid.xutils.DbUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShoppingCartActivity extends BaseActivity {
	
	public static final String ACTION_NAME = "UPDATE_DATA";
	public ListView mListView;
	public TextView sumPrice,zePrice;
	private Button settleAccounts;
	private RelativeLayout layoutBottom;
	private CheckBox checkbox;
	private ShoppingCartAdapter mAdapter;
	private List<ProducInfoModel> lists = new ArrayList<ProducInfoModel>();
	public DbUtils dbUtils;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shopping_cart, false, true);
		mListView = (ListView) findViewById(R.id.shoppingCartList);
		sumPrice = (TextView) findViewById(R.id.sum_price);
		zePrice = (TextView) findViewById(R.id.zePrice);
		settleAccounts = (Button) findViewById(R.id.settleAccounts);
		layoutBottom = (RelativeLayout) findViewById(R.id.layoutBottom);
		checkbox = (CheckBox) findViewById(R.id.checkbox);
		dbUtils = DbUtils.create(this);
		initData();
	}
	public void initData() {
		try {
			lists = dbUtils.findAll(ProducInfoModel.class);
			double price = 0;
			if(lists != null && lists.size() > 0){
				layoutBottom.setVisibility(View.VISIBLE);
				mAdapter = new ShoppingCartAdapter(lists, this);
				mListView.setAdapter(mAdapter);
				for (ProducInfoModel model : lists) {
					price += Double.valueOf(model.getPrice())*model.getBuyCount();
				}
				settleAccounts.setText("去结算("+lists.size()+")");
				sumPrice.setText("合计：¥"+price);
				zePrice.setText("总额：¥"+price);
			}else{
				layoutBottom.setVisibility(View.GONE);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void initLinster(){
		checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				
			}
		});
	}
	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){  
        @Override  
        public void onReceive(Context context, Intent intent) {  
            String action = intent.getAction();  
            if(action.equals(ACTION_NAME)){  
            	initData();
            }  
        }  
          
    }; 
    public void registerBoradcastReceiver(){  
        IntentFilter myIntentFilter = new IntentFilter();  
        myIntentFilter.addAction(ACTION_NAME);  
        //注册广播        
        registerReceiver(mBroadcastReceiver, myIntentFilter);  
    }  
    
    @Override
    protected void onDestroy() {
    	unregisterReceiver(mBroadcastReceiver);
    	super.onDestroy();
    }
	
}